﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuickApp;
using System.Windows.Input;
using System.ComponentModel;
using QuickApp.DbInterface;
using QuickApp.SqlSilver;
using QuickApp.DbInterface.Factories;

namespace QA.Wpf
{
    public class DbConnectionManagerViewModel : INotifyPropertyChanged
    {
        #region Private Fields

        private List<QADbConnection> connections = new List<QADbConnection>();

        private Dictionary<QADbConnection, QADbManager> connectToManager = new Dictionary<QADbConnection, QADbManager>();

        private bool isConnected;

        #endregion Private Fields

        #region Public Constructors

        public DbConnectionManagerViewModel()
        {
            addConnection(new SqlServerConnection("Home") { Server = ".\\SqlExpress", Database = "Phoenix", TrustedConnection = true });
            CurrentConnection = connections.FirstOrDefault();
        }

        #endregion Public Constructors

        #region Public Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Public Events

        #region Public Properties

        public List<QADbConnection> Connections
        {
            get { return connections; }
        }

        public QADbConnection CurrentConnection
        {
            get;
            set;
        }

        public QADbManager CurrentDbManager
        {
            get
            {
                if (CurrentConnection != null)
                {
                    QADbManager manager = connectToManager[CurrentConnection];
                    if (manager == null)
                    {
                        manager = DbManagerFactory.GetDbManager(CurrentConnection.DbType);
                        manager.SetConnection(CurrentConnection);
                        connectToManager[CurrentConnection] = manager;
                    }
                    return manager;
                }
                return null;
            }
        }

        public bool IsConnected
        {
            set
            {
                if (isConnected != value)
                {
                    isConnected = value;
                    onPropertyChanged("IsConnected");
                }
            }
            get
            {
                return isConnected;
            }
        }

        #endregion Public Properties

        #region Public Methods

        public void Connect()
        {
            if (CurrentConnection != null && !IsConnected && CurrentDbManager.CanConnect())
            {
                onPropertyChanged("CurrentDbManager");
                IsConnected = true;
            }
        }

        #endregion Public Methods

        #region Private Methods

        private void addConnection(QADbConnection connection)
        {
            connections.Add(connection);
            connectToManager.Add(connection, null);
        }
        private void onPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        #endregion Private Methods

    }


}

